package com.kelvin.paopao.module.peisong.mypeisong.peisongperson;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.PeisongModel;
import com.kelvin.paopao.api.responseBean.UserDetailModel;
import com.kelvin.paopao.widgets.EmptyLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/31.
 */

public class PeisongPersonPresenter implements IPeisongPersonPresenter {

    private PeisongPersonActivity mView;

    public PeisongPersonPresenter(PeisongPersonActivity view) {
        this.mView = view;
    }

    @Override
    public void getPeisongPerson(final String oId) {
        RetrofitService.getPeisongPerson(oId).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mView.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<UserDetailModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<UserDetailModel>
                                               commonListResponse) throws Exception {
                        mView.hideLoading();
                        if (commonListResponse.getResult() != null &&
                                commonListResponse.getResult().size() != 0) {
                            mView.loadData(commonListResponse.getResult());
                            boolean isCanCheck = true;
                            for (UserDetailModel model : commonListResponse.getResult()) {
                                if (model.getStates() == 1) {
                                    isCanCheck = false;
                                }
                            }
                            mView.loadCheck(isCanCheck);
                        } else {
                            mView.showNoData();
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mView.showNetError(new EmptyLayout.OnRetryListener() {
                            @Override
                            public void onRetry() {
                                getPeisongPerson(oId);
                            }
                        });
                    }
                });
    }

    @Override
    public void peisong(String user_no, final String oId) {
        RetrofitService.peisongConfirm(user_no, oId).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    ToastUtil.showToast(mView, "选定配送者成功");
                } else {
                    ToastUtil.showToast(mView, "选定配送者失败");
                }
                getPeisongPerson(oId);
            }
        });
    }


    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }
}
